package com.meiqijiacheng.sango.net;

import com.qlslylq.ad.sdk.constant.UrlConst;
import com.qlslylq.ad.sdk.util.Log;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;

import okhttp3.Dns;

/**
 * DNS重定向，配合Okhttp使用
 */
public class HttpDns implements Dns {

    private static final String TAG = HttpDns.class.getSimpleName();

    private String mDns;

    public HttpDns(String dns) {
        mDns = dns;
    }

    @Override
    public List<InetAddress> lookup(String hostname) {
        List<InetAddress> inetAddresses = null;
        try {
            inetAddresses = Dns.SYSTEM.lookup(hostname);
            if (inetAddresses == null) {
                if (hostname.equals(UrlConst.BASE_URL_RELEASE)) {
                    return Arrays.asList(InetAddress.getAllByName(mDns));
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
            Log.e(TAG, "dns解析错误");
        }
        return inetAddresses;
    }
}
